$(function () {
    initDg();
});

function initCombo() {
    var gameId = $("#gameId").val();
    $('#fdGameId').combobox({
        url: '/gameInfo/comboList',
        valueField: 'fdId',
        textField: 'fdName',
        editable: false,
        method : "post",
        onLoadSuccess: function (data) { //加载完成后,设置选中第一项
            var index = 0;
            for (var i = 0; i < data.length; i++) {
                if (data[i].fdId === gameId) {
                    index = i;
                }
            }
            $('#fdGameId').combobox('select', data[0].fdId);
        },
        onSelect: function (record) {
            $("#fdGameName").val(record.fdName);
        }
    });

}

function initDg() {
    $('#dg').datagrid({
        width: '100%',
        height: 'auto',
        url: '/raceInfo/list',
        fitColumns: true,
        autoRowHeight: false,
        striped: true,
        method: "GET",
        idField: "fdId",
        loadMsg: "正在加载数据...",
        pagination: true,
        rownumbers: true,
        pagePosition: 'bottom',
        pageSize: 50,
        pageList: [20, 30, 40, 50],
        showFooter: true,
        fit: true,
        queryParams: {
            name: $('#username').val()
        },
        columns: [[
            {field: 'fdId', title: 'ID', width: 100, align: 'center', checkbox: true},
            {
                field: 'fdName',
                width: 100,
                title: '赛事名称'
            },
            {
                field: 'fdDesc',
                width: 100,
                title: '赛事描述'
            },
            {
                field: 'fdStartTime',
                width: 100,
                title: '赛事开始时间'
            },
            {
                field: 'fdEndTime',
                width: 100,
                title: '赛事结束时间'
            },
            {
                field: 'fdGameName',
                width: 100,
                title: '所属游戏'
            }
        ]],
        toolbar: '#tb'
    });
}

function edit() {
    var row = $('#dg').datagrid('getSelected');
    if (row === null) {
        $.messager.alert('提示', '请选中一行数据操作!', 'Warning');
    } else {
        var id = row.fdId;
        $('#win').window({
            width: 600,
            height: 400,
            modal: true,
            collapsible: false,
            minimizable: false,
            maximizable: false,
            resizable: true,
            iconCls: "icon-add",
            href: "/raceInfo/edit/" + id,
            title: "编辑"
        });
        // initCombo();
    }
}

function add() {
    $('#win').window({
        width: 600,
        height: 400,
        modal: true,
        collapsible: false,
        minimizable: false,
        maximizable: false,
        resizable: true,
        iconCls: "icon-add",
        href: "add.jsp",
        title: "添加"
    });
    // initCombo();
}

function remove() {
    var row = $('#dg').datagrid('getSelections');
    if (row.length === 0) {
        $.messager.alert('提示', '请选中一行数据操作!', 'Warning');
    } else {
        var ids = new Array();
        $.each(row, function (i, row) {
            ids[i] = row['fdId'];
        });
        $.messager.confirm('提示', '确认删除该条记录?', function (r) {
            if (r) {
                $.ajax({
                    type: 'POST',
                    url: '/raceInfo/remove',
                    data: {"ids": ids},
                    success: function (r) {
                        if (r.code == 0) {
                            $.messager.alert('提示', r.msg, 'info');
                            reLoad();
                            clearSelections();
                        } else {
                            $.messager.alert('提示', r.msg, 'Warning');
                        }
                    }
                });
            }
        });
    }
}

function submitForm() {
    $('#ff').form('submit', {
        onSubmit: function () {
            return $(this).form('enableValidation').form('validate');
        },
        success: function (data) {
            var r = JSON.parse(data);
            if (r.code == 0) {
                $.messager.alert("提示", r.msg, "info");
                closeWin();
                reLoad();
                clearSelections();
            } else {
                $.messager.alert("提示", r.msg, "Warning")
            }
        }
    });
}

function clearForm() {
    $('#ff').form('clear');
}

function reLoad() {
    initDg();
}

function closeWin() {
    $("#win").window("close");
}

function clearSelections() {
    $("#dg").datagrid("clearSelections");
}
